<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单牌号" prop="orderMork">
        <el-input
          v-model="queryParams.orderMork"
          placeholder="请输入订单牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="公司名" prop="orderCompany">
        <el-input
          v-model="queryParams.orderCompany"
          placeholder="请输入公司名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="负责人" prop="orderPrincipal">
        <el-input
          v-model="queryParams.orderPrincipal"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crm:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单id" align="center" prop="orderId" />
      <el-table-column label="订单牌号" align="center" prop="orderMork" />
      <el-table-column label="数量" align="center" prop="orderNumber" />
      <el-table-column label="单位" align="center" >千克</el-table-column>
      <el-table-column label="单价" align="center" prop="orderPrice" />
      <el-table-column label="总价" align="center" prop="orderTotalPrice" />
      <el-table-column label="客户公司名" align="center" prop="orderCompany" />
      <el-table-column label="订单图片" align="center" prop="orderImg" >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.orderImg"
            :preview-src-list="scope.row.orderImgList">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="订单负责人" align="center" prop="orderPrincipal" />
      <el-table-column label="订单状态" align="center" >
        <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.orderStatus==='UNFILLEDORDERS' ">未发货</el-tag>
            <el-tag type="success" v-if="scope.row.orderStatus==='SHIPPED' ">已发货</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:order:edit']"
            v-if="scope.row.orderStatus==='UNFILLEDORDERS' "
          >发货</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单牌号" prop="orderMork">
          <el-input v-model="form.orderMork" placeholder="请输入订单牌号" />
        </el-form-item>
        <el-form-item label="数量单位公斤" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入数量单位公斤" />
        </el-form-item>
        <el-form-item label="单价" prop="orderPrice">
          <el-input v-model="form.orderPrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="总价" prop="orderTotalPrice">
          <el-input v-model="form.orderTotalPrice" placeholder="请输入总价" />
        </el-form-item>
        <el-form-item label="客户公司名" prop="orderCompany">
          <el-input v-model="form.orderCompany" placeholder="请输入客户公司名" />
        </el-form-item>
        <el-form-item label="客户id" prop="clientId">
          <el-input v-model="form.clientId" placeholder="请输入客户id" />
        </el-form-item>
        <el-form-item label="订单图片" prop="orderImg">
          <el-upload
            class="avatar-uploader"
            action="/dev-api/crm/order/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="headers"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.orderImg" :src="form.orderImg" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
<!--          <el-input v-model="form.orderImg" placeholder="请输入订单图片地址" />-->
        </el-form-item>
        <el-form-item label="订单负责人" prop="orderPrincipal">
          <el-input v-model="form.orderPrincipal" placeholder="请输入订单负责人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/crm/order";
import {upload} from "../../../api/crm/order";
import {getToken} from "../../../utils/auth";

export default {
  name: "Order",
  data() {
    return {
      headers:{
        Authorization: 'Bearer ' +getToken()
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderMork: null,
        orderNumber: null,
        orderPrice: null,
        orderTotalPrice: null,
        orderCompany: null,
        clientId: null,
        orderImg: null,
        orderPrincipal: null,
        orderStatus: null,
        creationBy: null,
        upadteBy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    uploadurl(file){
      upload(file).then(response => {
        console.log(response)
      })
    },

    //订单图片上传
    handleAvatarSuccess(response,res, file) {
      this.form.orderImg = response.msg
    },
    beforeAvatarUpload(file) {
      const isLt5M = file.size / 1024 / 1024 < 5;

      if (!isLt5M) {
        this.$message.error('上传头像图片大小不能超过 5MB!');
      }
      return isLt5M;
    },
    /** 查询客户订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        for (let i = 0; i < this.orderList.length; i++) {
           this.orderList[i]["orderImgList"]=[this.orderList[i].orderImg]
           console.log(this.orderList[i])
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderMork: null,
        orderNumber: null,
        orderPrice: null,
        orderTotalPrice: null,
        orderCompany: null,
        clientId: null,
        orderImg: null,
        orderPrincipal: null,
        orderStatus: null,
        createTime: null,
        creationBy: null,
        updateTime: null,
        upadteBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除客户订单编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>


<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
